package com.hl.apple.myapplication

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_jobdetail.*
import org.jsoup.Jsoup

class JobDeatilActivity : AppCompatActivity() {

    var jobnames = ""
    var salary = ""
    var edu = ""
    var addr = ""
    var expr = ""
    var neednum = ""
    var joblevel = ""
    var companyname = ""
    var companysize = ""


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_jobdetail)
        var url = intent.getStringExtra("url")


        Thread(Runnable {
            kotlin.run {
                getData(url)
            }
        }).start()


    }


    var handler: Handler = @SuppressLint("HandlerLeak")
    object : Handler() {
        override fun handleMessage(msg: Message) {
            // 处理消息
            super.handleMessage(msg)
            when (msg.what) {
                1 -> {
                    jobname!!.text = jobnames
                    jobmoney.text = salary
                    jobneed.text = "$addr   $expr   $edu"
                    company_name.text = companyname
                    company_size.text = companysize

                }

            }
        }
    }


    private fun getData(url: String) {

//        try {

        println("连接嗲值" + url)


        val doc = Jsoup.connect(url).get()
////            val connect = Jsoup.connect("http://info.bet007.com/cn/team/Summary.aspx?TeamID=35")
//        val header = HashMap<String, String>()
//        header["Host"] = "https://jobs.zhaopin.com"
//        header["User-Agent"] = "  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0"
//        header["Accept"] = "  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
//        header["Accept-Language"] = "zh-cn,zh;q=0.5"
//        header["Accept-Charset"] = "  GB2312,utf-8;q=0.7,*;q=0.7"
//        header["Connection"] = "keep-alive"
//        val data = doc.headers(header)
//        var document = data.get()
        jobnames = doc.getElementsByClass("inner-left fl")[0].getElementsByTag("h1").text()
        salary = doc.getElementsByClass("terminalpage-left")[0].getElementsByClass("terminal-ul clearfix")[0].getElementsByTag("strong")[0].text()
        addr = doc.getElementsByClass("terminalpage-left")[0].getElementsByClass("terminal-ul clearfix")[0].getElementsByTag("strong")[1].text()
        var strong = doc.getElementsByClass("terminalpage-left")[0].getElementsByClass("terminal-ul clearfix")[0].getElementsByTag("strong")
        expr = doc.getElementsByClass("terminalpage-left")[0].getElementsByClass("terminal-ul clearfix")[0].getElementsByTag("strong")[4].text()
        neednum = doc.getElementsByClass("terminalpage-left")[0].getElementsByClass("terminal-ul clearfix")[0].getElementsByTag("strong")[6].text()
        edu = doc.getElementsByClass("terminalpage-left")[0].getElementsByClass("terminal-ul clearfix")[0].getElementsByTag("strong")[6].text()
        joblevel = doc.getElementsByClass("terminalpage-left")[0].getElementsByClass("terminal-ul clearfix")[0].getElementsByTag("strong")[7].text()
        companyname = doc.getElementsByClass("terminalpage-right")[0].getElementsByClass("company-name-t")[0].text()

        companysize = doc.getElementsByClass("terminalpage-right")[0].getElementsByClass("terminal-ul clearfix terminal-company mt20")[0]
                .getElementsByTag("strong")[0].text()


        System.out.println("岗位名称$jobnames")
        println("工资${salary}")
        println("地点$addr")
        println("经验" + expr)
        println("需要" + neednum)
        println("学历" + edu)
        println("职位" + joblevel)
        println("公司名称：$companyname")
        println("公司规模$companysize")
        println("有枪" + strong.text())

        var msg = Message()
        msg.what = 1
        handler.sendMessage(msg)


    }


}